using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Customers
{
    [Authorize]
    public class CustomerAppService : BasicArchivesAppService, ICustomerAppService
    {
        protected ICustomerRepository CustomerRepository => LazyServiceProvider.LazyGetRequiredService<ICustomerRepository>();
        protected CustomerManager CustomerManager => LazyServiceProvider.LazyGetRequiredService<CustomerManager>();

        [Authorize(BasicArchivesPermissions.Customer.Query)]
        public async Task<CustomerDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<Customer, CustomerDto>(
                await CustomerRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.Customer.Query)]
        public async Task<CustomerDto> FindAsync(string filter)
        {
            var entity = await CustomerRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<Customer, CustomerDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Customer.Query)]
        public async Task<CustomerDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await CustomerRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<Customer, CustomerDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Customer.Query)]
        public async Task<PagedResultDto<CustomerDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await CustomerRepository.GetCountAsync(input.Filter, true);
            var list = await CustomerRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<CustomerDto>(
                totalCount,
                ObjectMapper.Map<List<Customer>, List<CustomerDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Customer.Query)]
        public async Task<PagedResultDto<CustomerDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<Customer, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<Customer>(input.FilterGroup);
            }

            var totalCount = await CustomerRepository.GetCountAsync(expression, true);
            var list = await CustomerRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<CustomerDto>(
                totalCount,
                ObjectMapper.Map<List<Customer>, List<CustomerDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Customer.Create)]
        public async Task<CustomerDto> CreateAsync(CustomerCreateDto input)
        {
            var entity = ObjectMapper.Map<CustomerCreateDto, Customer>(input);

            await CustomerManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Customer.Update)]
        public async Task<CustomerDto> UpdateAsync(Guid id, CustomerUpdateDto input)
        {
            var entity = await CustomerRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await CustomerManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Customer.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await CustomerManager.DeleteAsync(id);
        }
    }
}